<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/22
 * Time: 16:10
 */

namespace app\api\service;

use app\api\model\Recommend as RecommendModel;
use app\api\model\Task as TaskModel;
use app\api\model\User as UserModel;
use think\Db;


class Task
{
    public function test()
    {

    }

    /**
     * 任务刷新
     * @param $task_id
     * @return TaskModel
     */
    public static function refresh($task_id)
    {
        $time = (new TaskModel())->where(['id' => $task_id])->value('refresh_time');
        $data = [
            'id' => $task_id,
            'update_time' => time(),
            'refresh_time' => $time + 1
        ];
        $res = TaskModel::update($data);
        return $res;
    }

    /**
     * 计算任务刷新所需金币
     * @param $task_id
     * @param $user_id
     * @return float|int
     * @throws \think\exception\DbException
     */
    public static function refreshNeedGold($task_id, $user_id)
    {
        //
//        $task = TaskModel::getOne($task_id);
        $task = TaskModel::get(['id' => $task_id, 'user_id' => $user_id]);
        $user = UserModel::get($user_id);
        $refresh_interval = config('config.任务刷新_间隔_level_' . $user['level']);
        //时间段
        $period = ceil((time() - $task['add_time']) / 3600 / $refresh_interval);
        //判断和上次刷新是否在同一时间段
        if ($period == $task['refresh_period']) {
            //计算该时间段刷新所需金币
            if ($task['refresh_time'] == 0) {
                $gold = 0;
            } else {
                $refresh_gold_base = config('config.任务刷新_金币基数_level_' . $user['level']);
                $gold = $refresh_gold_base * pow(config('config.任务刷新_翻倍基数'), $task['refresh_time'] - 1);
            }
        } else {
            //更新refresh_hours
            $cond['id'] = $task_id;
            $data['refresh_period'] = $period;
            $data['refresh_time'] = 0;
            TaskModel::update($data, $cond);
            $gold = 0;
        }
        return $gold;
    }

    //验证任务是否属于该用户
    public static function checkUser($task_id, $user_id)
    {
        $res = TaskModel::get(['id' => $task_id, 'user_id' => $user_id]);
        return $res ? true : false;
    }

    /**
     * 任务分红
     * @param $user_id
     * @param $task_id
     * @param bool $type  true 加粉任务   false 发圈任务
     * @throws \think\exception\DbException
     */
    public static function reward($user_id, $task_id,$type = true)
    {
        $user = UserModel::get($user_id);
        $pid = $user['parentid'];
        //完成任务自己奖励
        $user->money = $user['money'] + config('config.任务_完成奖励金额');
        $user->total_money = $user['total_money'] + config('config.任务_完成奖励金额');
        $user->wait_money = $user['wait_money'] - config('config.任务_完成奖励金额');
        $user->finish_task = $user['finish_task'] + 1; //增加完成任务数
        $user->save();
        if ($type){
            $task_no = TaskModel::getValue(['id' => $task_id], 'number');
            $title = '完成加粉任务【' . $task_no . '】奖励';
            $task_type = 1;
        }else{
            $title = '完成发圈任务【' . $task_id . '】奖励';
            $task_type = 2;
        }
        //奖励推送
        $user_push_info = [
            'alert' => $title . config('config.任务_完成奖励金额')
        ];
        Push::income($user_id, $user_push_info);
        //余额增加日志
        $log_data[] = [
            'title' => $title,
            'user_id' => $user_id,
            'task_id' => $task_id,
            'task_type'=>$task_type,
            'type' => 1,
            'amount' => config('config.任务_完成奖励金额'),
            'add_time' => time(),
        ];
        if ($pid) {
            $p_user = UserModel::get($pid);
            $p_user->money = $p_user['money'] + config('config.任务_完成一级推荐奖');
            $p_user->total_money = $p_user['total_money'] + config('config.任务_完成一级推荐奖');
            $p_user->wait_money = $p_user['wait_money'] - config('config.任务_完成一级推荐奖');
            $log_data[] = [
                'title' => $user['nickname'] . '(ID:' . $user_id . ')'.$title,
                'user_id' => $pid,
                'task_id' => $task_id,
                'task_type'=>$task_type,
                'type' => 1,
                'amount' => config('config.任务_完成一级推荐奖'),
                'add_time' => time(),
            ];
            //奖励推送
            $p_push_info = [
                'alert' => '直推会员' . $user['nickname'] . '(ID:' . $user_id . ')'.$title . config('config.任务_完成一级推荐奖'),
            ];
            Push::income($pid, $p_push_info);
            $p_user->save();
        }
        //推广员奖励
        $promoter_id = RecommendModel::getPromoterID($user_id);
        if ($promoter_id) {
//            UserModel::where('id', $promoter_id)->update('money', config('config.任务_完成推广员奖励'));
            $pro_user = UserModel::get($promoter_id);
            $pro_user->total_money = $pro_user['total_money'] + config('config.任务_完成推广员奖励');
            $pro_user->money = $pro_user['money'] + config('config.任务_完成推广员奖励');
            $pro_user->wait_money = $pro_user['wait_money'] - config('config.任务_完成推广员奖励');
            $log_data[] = [
                'title' =>  '团队会员' .$user['nickname'] . '(ID:' . $user_id . ')'.$title,
                'user_id' => $promoter_id,
                'task_id' => $task_id,
                'task_type'=>$task_type,
                'type' => 1,
                'amount' => config('config.任务_完成推广员奖励'),
                'add_time' => time(),
            ];
            //奖励推送
            $pro_push_info = [
                'alert' => '团队会员' . $user['nickname'] . '(ID:' . $user_id . ')'. $title . config('config.任务_完成推广员奖励'),
            ];
//            Push::income($promoter_id, $pro_push_info);
            $pro_user->save();
        }
        if (!empty($log_data)) {
            Db::name('money')->insertAll($log_data);
        }
    }

    //判断任务是是否完成
    public static function isFinish($task_id)
    {
        $task = TaskModel::get($task_id);
        if ($task['finish'] >= $task['need']) {
            $task->status = config('status.任务_已完成');
            $task->save();
        }
    }

    /**
     * 结算预结算金额
     * @param $user
     * @throws \think\Exception
     */
    public static function operationWaitMoney($user)
    {
        //增加待结算
        $user->task_time = $user['task_time'] + 1; //增加当日任务次数
        $user->wait_money = $user['wait_money'] + config('config.任务_完成奖励金额');
        $user->save();
        //增加上级待结算金额
        UserModel::where('id', $user['parentid'])->setInc('wait_money', config('config.任务_完成一级推荐奖')); //待结算
        //增加脉大师待结算金额
        $promoter_id = RecommendModel::getPromoterID($user['id']);
        UserModel::where('id', $promoter_id)->setInc('wait_money', config('config.任务_完成一级推荐奖'));//待结算
    }
}